package com.honglv.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.constant.LikeContext;
import com.honglv.context.BaseContext;
import com.honglv.mapper.NewsMapper;
import com.honglv.pojo.dto.GetNewsDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.vo.*;
import com.honglv.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * Description:分页获取资讯
     * date: 2024/04/01
     * author:王铭杰
     */
    public PageVO<GetAllNewsVO> getAllNews(PageDTO pageDTO) {
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getPageSize());

        List<GetAllNewsVO> list = newsMapper.getAllNews();
        if (list == null){
            return null;
        }
        PageInfo<GetAllNewsVO> pageInfo = new PageInfo<>(list);
        return new PageVO<GetAllNewsVO>(pageInfo.getTotal(), pageInfo.getList());
    }


    /**
     * Description:获取点赞量前5条资讯
     * date: 2024/04/01
     * author:王铭杰
     */
    public List<GetTopNewsVO> getTopNews() {
        List<GetTopNewsVO> list = newsMapper.getTopNews();
        return list;
    }

    /**
     * Description:获取资讯详情页
     * date: 2024/04/01
     * author:王铭杰
     */
    public GetNewsVO getNews(GetNewsDTO getNewsDTO) {
        GetNewsVO getNewsVO = newsMapper.getNews(getNewsDTO.getId());
        if(getNewsVO == null){
            return null;
        }
        //        public static final String[] likeStatus = {"rounte","attractions","strategy","news"};
        String key = LikeContext.likeStatus[3] + getNewsDTO.getId();
        Long userId = BaseContext.getCurrentId();
        boolean identify = Boolean.TRUE.equals(stringRedisTemplate.opsForSet().isMember(key, String.valueOf(userId)));
        Integer likeStatus = identify? 1 : 0;
        getNewsVO.setLikeStatus(likeStatus);
        return getNewsVO;
    }


    /**
     * Description:通过用户id获取资讯
     * date: 2024/04/01
     * author:王铭杰
     */
    public List<GetAllNewsVO> getNewsByAuthorId(Long userId) {
        List<GetAllNewsVO> list = newsMapper.getNewsByAuthorId(userId);
        return list;
    }
}
